package com.umeox.watch.moto.dataservice.base;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.BatteryManager;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;
import android.text.TextUtils;

import androidx.annotation.RequiresApi;
import androidx.multidex.MultiDex;

import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.mmkv.MMKV;
import com.umeox.moto.common.log.Logger;
import com.umeox.moto.common.utils.AndroidUtils;
import com.umeox.watch.moto.dataservice.BuildConfig;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.dataservice.db.AppDataBase;

import java.util.concurrent.atomic.AtomicLong;


public class BaseApp extends Application {
    public static final String TAG_LAUNCHER = "device_launcher";
    private static BaseApp instance;
    public String deviceId;
    private AtomicLong mCount;
    private MMKV mmkv;
    private String holderId;

    public String createRequestId() {
        if (mCount != null) {
            return mCount.incrementAndGet() + "";
        } else {
            mCount = new AtomicLong();
            return mCount.incrementAndGet() + "";
        }
    }

    public MMKV getMmkv() {
        if (mmkv == null) {
            mmkv = MMKV.defaultMMKV();
        }
        return mmkv;
    }

    public String getHolderId() {
        if (!TextUtils.isEmpty(holderId)) {
            return holderId;
        }

        holderId = AppDataBase.getInstance().holderInfoDao().getHolderId();
        return holderId;
    }

    public int getBatteryLevel() {

        return LauncherManager.getInstance().getBatteryLevel();
    }

    public int isCharging() {
        Intent batteryBroadcast = registerReceiver(null,
                new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        // 0 means we are discharging, anything else means charging
        boolean isCharging = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) != 0;
        Logger.d("isCharging = " + isCharging);
        return isCharging ? 1 : 0;
    }

    public static BaseApp getInstance() {
        return instance;
    }

    //判断设备是否激活
    public static boolean isActivated() {
        return DataProvider.isActivated();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
        instance = this;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mCount = new AtomicLong();
        DataProvider.initialize(this);
        MMKV.initialize(this);
        LauncherManager.getInstance().initialize(this);
        CrashReport.UserStrategy userStrategy = new CrashReport.UserStrategy(this);
        userStrategy.setAppChannel(BuildConfig.FLAVOR + "-" + AndroidUtils.getDeviceId());
        CrashReport.initCrashReport(this, "a018aa68d5", false, userStrategy);
        CrashHandler.getInstance().init(this);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        LauncherManager.getInstance().destroy();
    }


    /**
     * 初始化语聊好友数据
     */
    public void setupChatDataBaseService() {
        Intent intent = new Intent();
        intent.setAction("com.umeox.watch.data.provider.DATABASESERVICE");
        intent.setPackage("com.umeox.watch.moto.chat");
        startService(intent);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private boolean isIgnoringBatteryOptimizations() {
        boolean isIgnoring = false;
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        if (powerManager != null) {
            isIgnoring = powerManager.isIgnoringBatteryOptimizations(getPackageName());
        }
        return isIgnoring;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void requestIgnoreBatteryOptimizations() {
        try {
            Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + getPackageName()));
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
